#include <iostream>
#include <string>
#include <algorithm>
#include <stack>

using namespace std;

// 1717. 删除子字符串的最大得分
// https://leetcode.cn/problems/maximum-score-from-removing-substrings/description/?envType=daily-question&envId=2025-07-23

class Solution
{
public:
    int maximumGain(string s, int x, int y)
    {
        if(y > x){
            swap(x, y);
            reverse(s.begin(), s.end());
        }
        stack<char> chStack;
        int ans = 0;
        for (int i = 0; i < s.size(); i++)
        {
            char curCh = s[i];
            if (curCh == 'b' && !chStack.empty() && chStack.top() == 'a')
            {
                chStack.pop();
                ans += x;
                continue;
            }
            chStack.push(curCh);
        }
        stack<char> temp;
        while (!chStack.empty())
        {
            char curCh = chStack.top();
            chStack.pop();
            if(!temp.empty() && curCh == 'b' && temp.top() == 'a'){
                temp.pop();
                ans += y;
                continue;
            }
            temp.push(curCh);
        }
        return ans;
    }
};

int main()
{
    return 0;
}